# Contributor: Díaz Urbaneja Diego <sodomon2@gmail.com>
# Maintainer: Díaz Urbaneja Diego <sodomon2@gmail.com>
pkgname=virtualbox
pkgver=6.1.50
pkgrel=0
pkgdesc="General-purpose full virtualizer for x86 hardware"
url="https://www.virtualbox.org"
arch="x86_64" # TODO
license="GPL-2.0-only AND CDDL-1.0"
install="$pkgname.pre-install"
depends="
	alsa-lib
	device-mapper-libs
	glu
	mesa
	libcap
	libcurl
	libvpx
	libxslt
	libtpms
	libxrandr
	libxcursor
	libxext
	linux-pam
	libxcb
	lvm2
	opus
	qt5-qtbase
	qt5-qtx11extras
	qt5-qttools
	sdl12-compat
	$pkgname-modules-vboxdrv
	$pkgname-modules-vboxnetadp
	$pkgname-modules-vboxnetflt
	"
makedepends="
	alsa-lib-dev
	bison
	curl-dev
	flex
	glib-dev
	glu-dev
	iasl
	kbuild
	mesa-dev
	libvncserver-dev
	libcap-dev
	libvpx-dev
	libxcomposite-dev
	libxinerama-dev
	libxslt-dev
	libxcursor-dev
	libxrandr-dev
	lvm2-dev
	libxmu-dev
	linux-pam-dev
	opus-dev
	pulseaudio-dev
	qt5-qtbase-dev
	qt5-qtx11extras-dev
	qt5-qttools-dev
	sdl12-compat-dev
	xorg-server-dev
	yasm
	"
# !check: fails with validationkit-build-linux and the postcheck()
# textrels: for the .r0 and .fd files
options="!check textrels"
subpackages="$pkgname-modules-vboxdrv::noarch
	$pkgname-modules-vboxnetadp::noarch
	$pkgname-modules-vboxnetflt::noarch
	"
source="$pkgname-$pkgver.tar.bz2::https://download.virtualbox.org/virtualbox/$pkgver/VirtualBox-$pkgver.tar.bz2
	https://download.gnome.org/sources/libIDL/0.8/libIDL-0.8.14.tar.bz2
	003-enable-internal-getproto-functions-for-musl.patch
	013-Makefile.patch
	019-libxml-2-12.patch
	fix-libidl-and-binutils.patch
	glibc-symvers.patch
	musl-fix.patch
	musl-no-glibc.patch
	musl-off_t.patch
	musl-dlmopen.patch
	LocalConfig.kmk
	virtualbox.conf
	60-vboxdrv.rules
	"
builddir="$srcdir/VirtualBox-$pkgver"
_srcmod="$builddir/out/linux.amd64/release/bin/src"

prepare() {
	default_prepare
	rm -rf $builddir/kBuild/bin $builddir/tools
	cp "$srcdir"/LocalConfig.kmk LocalConfig.kmk

	# require libIDL for fix broken compiling with vbox-xpcom
	cd "$srcdir/libIDL-0.8.14"
	update_config_sub
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=$builddir/libidl
	make install
	rm -r "$srcdir/libIDL-0.8.14"
}

build() {
	unset LD_LIBRARY_PATH
	./configure --nofatal \
		--disable-docs \
		--disable-kmods \
		--disable-python \
		--disable-hardening \
		--disable-java \
		--enable-vde \
		--with-makeself=/bin/echo
		source ./env.sh

	LD_LIBRARY_PATH=libidl/lib/ kmk all
}

package() {
	cd out/linux.amd64/release/bin
	for dir in bin lib/virtualbox/components \
		share/virtualbox; do
		mkdir -p "$pkgdir"/usr/"$dir"
	done

	install -m0755 VBox.sh "$pkgdir"/usr/bin/VBox
	for f in VirtualBox VBoxManage VBoxSDL VBoxHeadless \
		VBoxBalloonCtrl; do
		ln -sf VBox "$pkgdir/usr/bin/$f"
	done

	# components
	install -m0755 components/* -t "$pkgdir"/usr/lib/virtualbox/components
	strip "$pkgdir"/usr/lib/virtualbox/components/*.so

	# libs
	install -m0755 *.so "$pkgdir"/usr/lib/virtualbox
	install -m0644 *.r0 *.fd "$pkgdir"/usr/lib/virtualbox

	# bin
	install -m0755 VBoxSDL VirtualBoxVM VBoxHeadless VBoxNetDHCP VBoxNetAdpCtl \
	VBoxNetNAT -t "$pkgdir"/usr/lib/virtualbox

	install -m0755 VirtualBox VBoxManage VBoxSVC VBoxExtPackHelperApp \
		VBoxXPCOMIPCD VBoxTestOGL VBoxBalloonCtrl \
		-t "$pkgdir"/usr/lib/virtualbox

	for f in VirtualBox VirtualBoxVM VBoxManage VBoxSDL VBoxHeadless \
		VBoxBalloonCtrl VBoxNetAdpCtl VBoxNetDHCP VBoxNetNAT VBoxSVC \
		VBoxExtPackHelperApp VBoxXPCOMIPCD; do
		strip "$pkgdir"/usr/lib/virtualbox/$f
		strip "$pkgdir"/usr/lib/virtualbox/*.so
	done

	# locales
	install -d -m0755 "$pkgdir/usr/lib/virtualbox/nls"
	install -m0755 nls/*.qm -t "$pkgdir/usr/lib/virtualbox/nls"

	# scripts
	install -m0755 VBoxCreateUSBNode.sh VBoxSysInfo.sh \
		-t "$pkgdir"/usr/share/virtualbox

	# icons and desktop
	install -D -m0644 VBox.png "$pkgdir"/usr/share/pixmaps/VBox.png
	install -D -m0644 virtualbox.desktop "$pkgdir"/usr/share/applications/virtualbox.desktop
	sed -i -e "s|Icon=VBox|Icon=VBox.png|" \
		"$pkgdir"/usr/share/applications/virtualbox.desktop
	install -Dm644 virtualbox.xml "$pkgdir"/usr/share/mime/packages/virtualbox.xml

	install -d "$pkgdir"/usr/share/icons/hicolor
	for i in icons/*; do
		ldir=$(basename "$i")
		install -d "$pkgdir/usr/share/icons/hicolor/$ldir/mimetypes"
		cp -a "$i"/* "$pkgdir/usr/share/icons/hicolor/$ldir/mimetypes"
	done

	# configuration file
	mkdir -p "$pkgdir"/etc/vbox
	echo 'INSTALL_DIR=/usr/lib/virtualbox' > "$pkgdir"/etc/vbox/vbox.cfg

	# licenses
	install -D -m0644 "$builddir"/COPYING "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
	install -D -m0644 "$builddir"/COPYING.CDDL "$pkgdir/usr/share/licenses/$pkgname/LICENSE.CDDL"

	# Install rules.d file.
	install -Dm0644 "$srcdir"/60-vboxdrv.rules "$pkgdir"/usr/lib/udev/rules.d/60-vboxdrv.rules

	# Install modules-load.d file.
	install -Dm0644 "$srcdir"/virtualbox.conf "$pkgdir"/usr/lib/modules-load.d/virtualbox.conf
}

vboxdrv() {
	pkgdesc="vboxdrv Kernel module source"
	depends="akms"

	mkdir -p "$subpkgdir"/usr/src/
	cd $_srcmod

	sed -i 's|obj ?= $(CURDIR)||g' "$_srcmod"/vboxdrv/Makefile
	sed -i 's|include $(obj)/Makefile-header.gmk|include /usr/src/vboxdrv/Makefile-header.gmk|g' \
		"$_srcmod"/vboxdrv/Makefile

	sed -i 's|include $(obj)/Makefile-footer.gmk|include /usr/src/vboxdrv/Makefile-footer.gmk|g' \
		"$_srcmod"/vboxdrv/Makefile

	cat >vboxdrv/AKMBUILD <<-EOF
	modname=vboxdrv
	modver=$pkgver-r$pkgrel
	built_modules='vboxdrv.ko'
	EOF

	# fix compiling vboxnetadp and vboxnetflt
	echo -e 'build() {
	make $MAKEFLAGS -C "$kernel_srcdir" M="$builddir" src="$srcdir" modules
	cp $builddir/Module.symvers $srcdir\n}' >> vboxdrv/AKMBUILD

 	mv vboxdrv/ "$subpkgdir"/usr/src/
 	chmod 777 $subpkgdir/usr/src/vboxdrv/
}

vboxnetadp() {
	pkgdesc="vboxnetadp Kernel module source"
	depends="$pkgname-modules-vboxdrv"

	mkdir -p "$subpkgdir"/usr/src/
	cd $_srcmod

	sed -i 's|obj ?= $(CURDIR)||g' "$_srcmod"/vboxnetadp/Makefile
	sed -i 's|include $(obj)/Makefile-header.gmk|include /usr/src/vboxnetadp/Makefile-header.gmk|g' \
		"$_srcmod"/vboxnetadp/Makefile

	sed -i 's|include $(obj)/Makefile-footer.gmk|include /usr/src/vboxnetadp/Makefile-footer.gmk|g' \
		"$_srcmod"/vboxnetadp/Makefile

	cat >vboxnetadp/AKMBUILD <<-EOF
	modname=vboxnetadp
	modver=$pkgver-r$pkgrel
	built_modules='vboxnetadp.ko'
	EOF

	mv vboxnetadp/ "$subpkgdir"/usr/src/
}

vboxnetflt() {
	pkgdesc="vboxnetflt Kernel module source"
	depends="$pkgname-modules-vboxdrv"

	mkdir -p "$subpkgdir"/usr/src/
	cd $_srcmod

	sed -i 's|obj ?= $(CURDIR)||g' "$_srcmod"/vboxnetflt/Makefile
	sed -i 's|include $(obj)/Makefile-header.gmk|include /usr/src/vboxnetflt/Makefile-header.gmk|g' \
		"$_srcmod"/vboxnetflt/Makefile

	sed -i 's|include $(obj)/Makefile-footer.gmk|include /usr/src/vboxnetflt/Makefile-footer.gmk|g' \
		"$_srcmod"/vboxnetflt/Makefile

	cat >vboxnetflt/AKMBUILD <<-EOF
	modname=vboxnetflt
	modver=$pkgver-r$pkgrel
	built_modules='vboxnetflt.ko'
	EOF

	mv vboxnetflt/ "$subpkgdir"/usr/src/
}

sha512sums="
ee2ec352e020f07262bf2120278d0d9807c501cd7c4d621d4e12ce18a821b134d48eb852bc7136008cfa77ed709703d3c2835b7a7177c325d989e8f433aff73f  virtualbox-6.1.50.tar.bz2
906dde087908acd64b209fe466a89ae52a6ea23601cb30092063a6e062b1b6640fc2d5f422efcdd52b2b36ce20e6c244da6205fde0bdcec787fe64058be7adac  libIDL-0.8.14.tar.bz2
8b2088958c5fa824bb3f0f9f119b1fe2a0e119a2954cecfc2427aace8bdf9c1930b512c153fc1dedb03466aa1793f2fc27ba32b3d8da061e34a28c83dc309e28  003-enable-internal-getproto-functions-for-musl.patch
6c8d4df7b8304646a27a456f4d3373b43dbb1d3a352056e70257965ab6db5317ec78360c25ddb193cb99d515f9acbfd5e57821728f96cd8539f71ae70ff13ff6  013-Makefile.patch
6cbdea2fbe991a0b8fdee77664ff1e2a3f8846510d4723ea992de6a05ef04dc35c167dd2f104bc65301a5712a06010436d58bcb50eef51a5ea6280e6510219b4  019-libxml-2-12.patch
29e0e25ba85419c22a304db9b12f73bbd980670ec6cad51854097ad87f01a654db60a8c83c95ac5b1d8e2a657b3bc9ae7152bc85788c458a583821b54e11fe2b  fix-libidl-and-binutils.patch
5e6ecb718b7239b9ca9c943d6427adf23b664156c91d8bce58747cfff8c975d221f2e3c1cd69790fd4c1d8b2425c2546bd77c187f666dc2f321ffeb8a06c7273  glibc-symvers.patch
166f059fd203ed51cb425c54bfdc466313934017864f3510fd39011845566b3e305d151a309d60eed122c51b4370896fa193d56c3f3553c2195cf5862ca0ce41  musl-fix.patch
1f8c8c1f5a0aedf84c1f3cd41c84d007d30e6502b0b8daaa60de3bee75c2bc0e1a259f6019f20eac40738ae39980e339d52467af6c99354d6909f94bfc701532  musl-no-glibc.patch
5ce315e0fdab8a3213d08150f4584c589c59f7c654c6525b6708dc2967b2382b3c231a2d4f8846dd218c1f576d8d8409302c16df07914583431d8817dc5172c4  musl-off_t.patch
19f6caaaee0004987cc4c4dd4a137a6364733ec1326f961b5755cd4c2968e9417e5e5ed62d05a6674c671caf9bfddd00a9ed97c2046b1f75fd1abdcb0cd9d475  musl-dlmopen.patch
b06318f77da986c59c96a37f1bf6c681f6eb70d8045b6678960315827f9419c00208d2165990792e12e9aad93abe280e417632a8eb40fdbb353f9d9950c0ed5c  LocalConfig.kmk
7cafb7eb3166641a3dbec551a77f7e00d6da59bceaebba401e7c9b34259fab00aa8b1bdd33e9b7ac4c3531811bdea6ce056a1de2e0694f2e90f1984924fb656d  virtualbox.conf
8f368adb7f7a90699c99d4df392d0539a4b3f23d83f6906a3aed7b3fd2ff011189934e415e2b4947e951893a6484812915ccaf7b8d4cf801fc66049371c1223f  60-vboxdrv.rules
"
